package com.example.syyao.myapp;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.syyao.myapp.Adapter.TestStackAdapter;
import com.example.syyao.myapp.Response.Get_response;
import com.example.syyao.myapp.Response.Get_response_List;
import com.example.syyao.myapp.Sign.Sign_in;
import com.example.syyao.myapp.WebContact.JsonUtil;
import com.example.syyao.myapp.WebContact.WebAccessTools;
import com.loopeer.cardstack.AllMoveDownAnimatorAdapter;
import com.loopeer.cardstack.CardStackView;
import com.loopeer.cardstack.UpDownAnimatorAdapter;
import com.loopeer.cardstack.UpDownStackAnimatorAdapter;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import static java.lang.Thread.sleep;


public class MainFragment extends Fragment implements CardStackView.ItemExpendListener {

    Get_response_List index_response;
    ArrayList<Bitmap> arrayList;
    ArrayList<String> imageurl;
    private CardStackView mStackView;
    //    private LinearLayout mActionButtonContainer;
    private TestStackAdapter mTestStackAdapter;
    private ImageView toPerson;
    private ImageView toPut;
    private List<Map<String,Object>> informartion;



    private List<View> views = new ArrayList<View>();

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_main, container, false);
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.fragment_main);


        mStackView = (CardStackView) view.findViewById(R.id.stackview_main);
        mStackView.setItemExpendListener(this);
        init();
//        try {
//            sleep(2000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//        System.out.println("--------------------------------"+informartion.size());
//
        toPerson = view.findViewById(R.id.toPerson);
        toPut = view.findViewById(R.id.toPut);

        toPerson.setOnClickListener(new toPersonClick());
        toPut.setOnClickListener(new toPutClick());

        imageurl=new ArrayList<>();
        arrayList=new ArrayList<>();

        imageurl.add("http://120.79.205.71/picture/pic1.jpg");
        imageurl.add("http://120.79.205.71/picture/pic2.jpg");
        imageurl.add("http://120.79.205.71/picture/pic3.jpg");
        imageurl.add("http://120.79.205.71/picture/pic4.jpg");



        return view;
    }
    void  init(){
        new Thread(new Runnable() {

            @Override
            public void run() {
                String webContent = WebAccessTools.indexInformation();
                System.out.println("webCount:"+webContent);
                index_response= (Get_response_List) JsonUtil.jsonToObject(webContent,
                        Get_response_List.class);
                if (index_response != null && index_response.getMessage().equals("成功")) {
                    informartion = new ArrayList<>();
                    informartion = index_response.getData();
                    System.out.println("size:"+informartion.size());
                }

                handler.sendEmptyMessage(0x123);
            }
        }).start();
    }



    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            mTestStackAdapter = new TestStackAdapter(getContext(),informartion);
            mStackView.setAdapter(mTestStackAdapter);

            mTestStackAdapter.updateData(imageurl);


        };
    };

//	@Override
//    public boolean onCreateOptionsMenu(Menu menu) {
//        getMenuInflater().inflate(R.menu.menu_actions, menu);
//        return super.onCreateOptionsMenu(menu);
//    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_all_down:
                mStackView.setAnimatorAdapter(new AllMoveDownAnimatorAdapter(mStackView));
                break;
            case R.id.menu_up_down:
                mStackView.setAnimatorAdapter(new UpDownAnimatorAdapter(mStackView));
                break;
            case R.id.menu_up_down_stack:
                mStackView.setAnimatorAdapter(new UpDownStackAnimatorAdapter(mStackView));
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    public void onPreClick(View view) {
        mStackView.pre();
    }

    public void onNextClick(View view) {
        mStackView.next();
    }

    public void onItemExpend(boolean expend) {
//        mActionButtonContainer.setVisibility(expend ? View.VISIBLE : View.GONE);
    }


    private class toPersonClick implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            final MainActivity mainActivity = (MainActivity) getActivity();
            mainActivity.setFragmentSkipInterface(new MainActivity.FragmentSkipInterface() {
                @Override
                public void gotoFragment(ViewPager viewPager) {
                    /** 跳转到第二个页面的逻辑 */
                    viewPager.setCurrentItem(2);
                }
            });
            /** 进行跳转 */
            mainActivity.skipToFragment();


        }
    }

    private class toPutClick implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            final MainActivity mainActivity = (MainActivity) getActivity();
            mainActivity.setFragmentSkipInterface(new MainActivity.FragmentSkipInterface() {
                @Override
                public void gotoFragment(ViewPager viewPager) {
                    /** 跳转到第零个页面的逻辑 */
                    viewPager.setCurrentItem(0);
                }
            });
            /** 进行跳转 */
            mainActivity.skipToFragment();
        }
    }

    public Bitmap getBitmap(String path) throws IOException {
        try {
            URL url = new URL(path);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setConnectTimeout(5000);
            conn.setRequestMethod("GET");
            if (conn.getResponseCode() == 200) {
                InputStream inputStream = conn.getInputStream();
                Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                return bitmap;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    private Drawable loadImageFromNetwork(String imageUrl) {
        Drawable drawable = null;
        try {
// 可以在这里通过文件名来判断，是否本地有此图片
            drawable = Drawable.createFromStream(new URL(imageUrl).openStream(), "image.jpg");
        } catch (IOException e) {
            Log.d("test", e.getMessage());
        }
        if (drawable == null) {
            Log.d("test", "null drawable");
        } else {
            Log.d("test", "not null drawable");
        }

        return drawable;
    }


}
